/*
 * Copyright 2008 aileron.cc, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.commons.accessor.properties;

import cc.aileron.commons.accessor.factory.PojoAccessorInstanceManager;
import cc.aileron.commons.exception.NoSuchAccessorRuntimeException;

/**
 * プロパティアクセスの為のアクセサ
 * 
 * @author Aileron
 * @param <T> 
 * 
 */
public class PojoPropertiesChainAccesserImpl<T> implements PojoPropertiesChainAccessor<T>
{
    /*
     * (非 Javadoc)
     * @see cc.aileron.commons.accessor.PojoPropertiesChainAccesser#setObjectChainAccess(java.lang.String, java.lang.Object)
     */
    public void setObjectChainAccess(final String name, final Object value) throws NoSuchAccessorRuntimeException
    {
        final String[] nameTokens = name.split("\\.");
        final int max = nameTokens.length - 1;

        int i = 0;
        PojoProperties<?> pojo = target;

        if (max == 0)
        {
            pojo.setObject(name, value);
            return;
        }

        while (i < max)
        {
            final String key = nameTokens[i++];
            final Object obj = pojo.getObject(key);
            pojo = obj==null?instanceManager.newProperties(pojo.newPropertieInstance(key)):instanceManager.newProperties(obj);
        }
        pojo.setObject(nameTokens[i], value);
    }

    /*
     * (非 Javadoc)
     * @see cc.aileron.commons.accessor.PojoPropertiesChainAccesser#getObjectChainAccess(java.lang.String)
     */
    public Object getObjectChainAccess(final String name) throws NoSuchAccessorRuntimeException
    {
        final String[] nameTokens = name.split("\\.");
        final int max = nameTokens.length;

        int i = 0;
        Object tmp = null;

        tmp = max > 0 ? target.getObject(nameTokens[i++]) : target.getObject(name);
        if (tmp == null)
        {
            return null;
        }
        while (tmp != null && i < max)
        {
            tmp = instanceManager.newProperties(tmp).getObject(nameTokens[i++]);
        }
        return tmp;
    }

    /*
     * (非 Javadoc)
     * 
     * @see cc.aileron.commons.pojo.PojoProperties#toString()
     */

    @Override
    public String toString()
    {
        return this.getClass().getName() + "$" + target.toTarget();
    }

    /*
     * (非 Javadoc)
     * @see cc.aileron.commons.accessor.PojoPropertiesChainAccesser#toTarget()
     */
    public T toTarget()
    {
        return target.toTarget();
    }

    /**
     * target object の設定
     * @param instanceManager 
     * @param target 
     */
    public PojoPropertiesChainAccesserImpl(
            final PojoAccessorInstanceManager instanceManager,
            final PojoProperties<T> target)
    {
        this.instanceManager = instanceManager;
        this.target = target;
    }
    
    /**
     * instance-manager
     */
    private final PojoAccessorInstanceManager instanceManager;
    
    /**
     * target
     */
    private final PojoProperties<T> target ;
}